package com.iaiai.cobra.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iaiai.cobra.common.constant.YesOrNo;
import com.iaiai.cobra.repository.bean.*;
import com.iaiai.cobra.repository.bean.po.UserPo;
import com.iaiai.cobra.repository.mapper.UserMapper;
import com.iaiai.cobra.system.service.UserDeptService;
import com.iaiai.cobra.system.service.UserPostService;
import com.iaiai.cobra.system.service.UserRoleService;
import com.iaiai.cobra.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with UserServiceImpl.
 * Package: com.iaiai.cobra.system.service.impl
 * Author: iaiai
 * Create Time: 2024/6/22 21:45
 * QQ: 176291935
 * Email: 176291935@qq.com
 * Description:
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserDeptService userDeptService;

    @Autowired
    private UserPostService userPostService;

    @Autowired
    private UserRoleService userRoleService;

    //查用户
    public User queryAccount(String account){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getDel, YesOrNo.NO.getValue());
        wrapper.eq(User::getAccount, account);

        return getBaseMapper().selectOne(wrapper);
    }

    //分页查询
    public Page<User> queryPage(Page page, String search){
        return getBaseMapper().queryPage(page,search);
    }

    @Transactional(rollbackFor = Exception.class)
    public User add(UserPo userPo){
        userPo.setDel(YesOrNo.NO.getValue());
        userPo.setCreateTime(new Date());
        userPo.setModifyTime(userPo.getCreateTime());
        getBaseMapper().insert(userPo);

        if(userPo.getDeptIds()!=null){
            for (Long deptId : userPo.getDeptIds()) {
                UserDept userDept = new UserDept();
                userDept.setUserId(userPo.getId());
                userDept.setDeptId(deptId);
                userDept.setDel(YesOrNo.NO.getValue());
                userDept.setCreateTime(new Date());
                userDept.setModifyTime(userDept.getCreateTime());
                userDeptService.save(userDept);
            }
        }

        if(userPo.getPostIds()!=null){
            for (Long postId : userPo.getPostIds()) {
                UserPost userPost = new UserPost();
                userPost.setUserId(userPo.getId());
                userPost.setPostId(postId);
                userPost.setDel(YesOrNo.NO.getValue());
                userPost.setCreateTime(new Date());
                userPost.setModifyTime(userPost.getCreateTime());
                userPostService.save(userPost);
            }
        }

        if(userPo.getRoleIds()!=null){
            for (Long roleId : userPo.getRoleIds()) {
                UserRole userRole = new UserRole();
                userRole.setUserId(userPo.getId());
                userRole.setRoleId(roleId);
                userRole.setDel(YesOrNo.NO.getValue());
                userRole.setCreateTime(new Date());
                userRole.setModifyTime(userRole.getCreateTime());
                userRoleService.save(userRole);
            }
        }

        return userPo;
    }

    @Transactional(rollbackFor = Exception.class)
    public User edit(UserPo userPo){
        userPo.setModifyTime(new Date());
        getBaseMapper().updateById(userPo);

        if(userPo.getPostIds()==null)userPo.setPostIds(new ArrayList<>());
        if(userPo.getDeptIds()==null)userPo.setDeptIds(new ArrayList<>());
        if(userPo.getRoleIds()==null)userPo.setRoleIds(new ArrayList<>());

        List<UserDept> userDepts = userDeptService.queryUser(userPo.getId());
        if(userDepts==null)userDepts = new ArrayList<>();
        List<Long> originDeptIds = userDepts.stream().map(UserDept::getDeptId).collect(Collectors.toList());

        //找出多余的元素
        List<Long> addDeptIds = userPo.getDeptIds().stream()
                .filter(element -> !originDeptIds.contains(element))
                .collect(Collectors.toList());

        // 找出被删除的元素
        List<Long> delDeptIds = originDeptIds.stream()
                .filter(element -> !userPo.getDeptIds().contains(element))
                .collect(Collectors.toList());

        if(addDeptIds!=null && addDeptIds.size()>0){
            for (Long addDeptId : addDeptIds) {
                UserDept userDept = new UserDept();
                userDept.setUserId(userPo.getId());
                userDept.setDeptId(addDeptId);
                userDept.setDel(YesOrNo.NO.getValue());
                userDept.setCreateTime(new Date());
                userDept.setModifyTime(userDept.getCreateTime());
                userDeptService.save(userDept);
            }
        }

        if(delDeptIds!=null && delDeptIds.size()>0){
            for (Long delDeptId : delDeptIds) {
                userDeptService.delUserDept(userPo.getId(),delDeptId);
            }
        }



        List<UserPost> userPosts = userPostService.queryUser(userPo.getId());
        if(userPosts==null)userPosts = new ArrayList<>();
        List<Long> originPostIds = userPosts.stream().map(UserPost::getPostId).collect(Collectors.toList());

        //找出多余的元素
        List<Long> addPostIds = userPo.getPostIds().stream()
                .filter(element -> !originPostIds.contains(element))
                .collect(Collectors.toList());

        // 找出被删除的元素
        List<Long> delPostIds = originPostIds.stream()
                .filter(element -> !userPo.getPostIds().contains(element))
                .collect(Collectors.toList());

        if(addPostIds!=null && addPostIds.size()>0){
            for (Long addPostId : addPostIds) {
                UserPost userPost = new UserPost();
                userPost.setUserId(userPo.getId());
                userPost.setPostId(addPostId);
                userPost.setDel(YesOrNo.NO.getValue());
                userPost.setCreateTime(new Date());
                userPost.setModifyTime(userPost.getCreateTime());
                userPostService.save(userPost);
            }
        }

        if(delPostIds!=null && delPostIds.size()>0){
            for (Long delPostId : delPostIds) {
                userPostService.delUserPost(userPo.getId(),delPostId);
            }
        }



        List<UserRole> userRoles = userRoleService.queryUser(userPo.getId());
        if(userRoles==null)userRoles = new ArrayList<>();
        List<Long> originRoleIds = userRoles.stream().map(UserRole::getRoleId).collect(Collectors.toList());

        //找出多余的元素
        List<Long> addRoleIds = userPo.getRoleIds().stream()
                .filter(element -> !originRoleIds.contains(element))
                .collect(Collectors.toList());

        // 找出被删除的元素
        List<Long> delRoleIds = originRoleIds.stream()
                .filter(element -> !userPo.getRoleIds().contains(element))
                .collect(Collectors.toList());

        if(addRoleIds!=null && addRoleIds.size()>0){
            for (Long addRoleId : addRoleIds) {
                UserRole userRole = new UserRole();
                userRole.setUserId(userPo.getId());
                userRole.setRoleId(addRoleId);
                userRole.setDel(YesOrNo.NO.getValue());
                userRole.setCreateTime(new Date());
                userRole.setModifyTime(userRole.getCreateTime());
                userRoleService.save(userRole);
            }
        }

        if(delRoleIds!=null && delRoleIds.size()>0){
            for (Long delRoleId : delRoleIds) {
                userRoleService.delUserRole(userPo.getId(),delRoleId);
            }
        }

        return userPo;
    }

}
